﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a trigonometric polynomial.
    /// </summary>
    [Serializable]
    public class TrigonometricPolynomial : AbstractRealPolynomial, ITrigonometricPolynomial
    {
        /// <summary>
        /// The constant value of the trigonometric polynomial.
        /// </summary>
        private double a0;

        /// <summary>
        /// The components of the trigonometric polynomial.
        /// </summary>
        private readonly List<TrigonometricPolynomialComponent> components;

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomial"/> class.
        /// </summary>
        public TrigonometricPolynomial()
        {
            this.components = new List<TrigonometricPolynomialComponent>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomial"/> class.
        /// </summary>
        /// <param name="polynomial">The trigonometric polynomial to clone.</param>
        public TrigonometricPolynomial(TrigonometricPolynomial polynomial)
        {
            if (polynomial == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("polynomial");
            }

            this.a0 = polynomial.A0;
            this.components = new List<TrigonometricPolynomialComponent>();

            for (int i = 0; i < polynomial.Degree; i++)
            {
                this[i] = polynomial[i].Copy();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomial"/> class.
        /// </summary>
        /// <param name="components">The components of the trigonometric polynomial.</param>
        public TrigonometricPolynomial(TrigonometricPolynomialComponent[] components)
        {
            if (components == (TrigonometricPolynomialComponent[]) null)
            {
                throw new ArgumentNullException("components");
            }

            this.components = new List<TrigonometricPolynomialComponent>();

            for (int i = 0; i < components.Length; i++)
            {
                this.components.Add(new TrigonometricPolynomialComponent(components[i]));
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomial"/> class.
        /// </summary>
        /// <param name="a0">The constant value a0 of the trigonometric polynomial.</param>
        /// <param name="components">The components of the trigonometric polynomial.</param>
        public TrigonometricPolynomial(double a0, TrigonometricPolynomialComponent[] components)
        {
            if (components == (TrigonometricPolynomialComponent[]) null)
            {
                throw new ArgumentNullException("components");
            }

            this.a0 = a0;
            this.components = new List<TrigonometricPolynomialComponent>();

            for (int i = 0; i < components.Length; i++)
            {
                this.components.Add(new TrigonometricPolynomialComponent(components[i]));
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomial"/> class.
        /// </summary>
        /// <param name="components">The components of the trigonometric polynomial.</param>
        public TrigonometricPolynomial(List<TrigonometricPolynomialComponent> components)
        {
            if (components == (List<TrigonometricPolynomialComponent>) null)
            {
                throw new ArgumentNullException("components");
            }

            this.components = new List<TrigonometricPolynomialComponent>();

            for (int i = 0; i < components.Count; i++)
            {
                this.components.Add(new TrigonometricPolynomialComponent(components[i]));
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TrigonometricPolynomial"/> class.
        /// </summary>
        /// <param name="a0">The constant value a0 of the trigonometric polynomial.</param>
        /// <param name="components">The components of the trigonometric polynomial.</param>
        public TrigonometricPolynomial(double a0, List<TrigonometricPolynomialComponent> components)
        {
            if (components == (List<TrigonometricPolynomialComponent>) null)
            {
                throw new ArgumentNullException("components");
            }

            this.a0 = a0;
            this.components = new List<TrigonometricPolynomialComponent>();

            for (int i = 0; i < components.Count; i++)
            {
                this.components.Add(new TrigonometricPolynomialComponent(components[i]));
            }
        }

        /// <summary>
        /// Gets or sets the constant value a0 of the trigonometric polynomial.
        /// </summary>
        /// <value>The a0.</value>
        public double A0
        {
            get { return a0; }
            set { a0 = value; }
        }

        /// <summary>
        /// Gets the degree of the trigonometric polynomial.
        /// </summary>
        /// <value>The degree of the trigonometric polynomial.</value>
        public int Degree
        {
            get { return components.Count; }
        }

        /// <summary>
        /// Gets or sets the <see cref="SmartMathLibrary.TrigonometricPolynomialComponent"/> at the specified index.
        /// </summary>
        /// <value>The trigonometric polynomial component at the specified position.</value>
        public TrigonometricPolynomialComponent this[int index]
        {
            get { return this.components[index]; }
            set { this.components[index] = value; }
        }

        /// <summary>
        /// Generates the random trigonometric polynomial.
        /// </summary>
        /// <param name="components">The number of components to generate.</param>
        /// <returns>A random trigonometric polynomial</returns>
        public static TrigonometricPolynomial GenerateRandomPolynomial(int components)
        {
            Random random = new Random();
            List<TrigonometricPolynomialComponent> randomComponents = new List<TrigonometricPolynomialComponent>();

            for (int i = 0; i < components; i++)
            {
                randomComponents.Add(new TrigonometricPolynomialComponent(random.NextDouble(),
                                                                          random.NextDouble()));
            }

            return new TrigonometricPolynomial(randomComponents);
        }


        /// <summary>
        /// Determines the trigonometric polynomial with the smallest rank.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial.</param>
        /// <param name="b">The second trigonometric polynomial.</param>
        /// <returns>The trigonometric polynomial with the smallest rank.</returns>
        public static TrigonometricPolynomial Min(TrigonometricPolynomial a, TrigonometricPolynomial b)
        {
            if (a == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Degree > b.Degree)
            {
                return b;
            }

            return a;
        }

        /// <summary>
        /// Determines the trigonometric polynomial with the biggest rank.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial.</param>
        /// <param name="b">The second trigonometric polynomial.</param>
        /// <returns>The trigonometric polynomial with the biggest rank.</returns>
        public static TrigonometricPolynomial Max(TrigonometricPolynomial a, TrigonometricPolynomial b)
        {
            if (a == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Degree < b.Degree)
            {
                return b;
            }

            return a;
        }

        /// <summary>
        /// Compares the current instance of a trigonometric polynomial to another one.
        /// </summary>
        /// <param name="compare">The trigonometric polynomial to compare.</param>
        /// <returns>True if the trigonometric polynomial are even otherwise, false.</returns>
        public bool CompareTo(TrigonometricPolynomial compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Adds a trigonometric polynomial component to the current instance.
        /// </summary>
        /// <param name="component">The component to add.</param>
        public void AddComponent(TrigonometricPolynomialComponent component)
        {
            this.components.Add(component);
        }

        /// <summary>
        /// Removes a trigonometric polynomial component from the current instance.
        /// </summary>
        /// <param name="index">The specified index, where the component should be removed.</param>
        public void RemoveComponentAt(int index)
        {
            this.components.RemoveAt(index);
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public TrigonometricPolynomial Copy()
        {
            TrigonometricPolynomial result = new TrigonometricPolynomial();

            for (int i = 0; i < this.Degree; i++)
            {
                result[i] = this[i].Copy();
            }

            return result;
        }

        /// <summary>
        /// Solves the trigonometric polynomial at the specified position x.
        /// </summary>
        /// <param name="x">The value for evaluation.</param>
        /// <returns>The result t(x) at the specified position x.</returns>
        public override double SolveAt(double x)
        {
            double result = 0;

            for (int i = 0; i < this.Degree; i++)
            {
                result += this[i].SolveAt(x, i + 1);
            }

            return this.a0 / 2 + result;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial.</param>
        /// <param name="b">The second trigonometric polynomial.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(TrigonometricPolynomial a, TrigonometricPolynomial b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            if (a.Degree == b.Degree)
            {
                if (a.A0 == b.A0)
                {
                    for (int i = 0; i < a.Degree; i++)
                    {
                        if (a[i] != b[i])
                        {
                            return false;
                        }
                    }

                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial.</param>
        /// <param name="b">The second trigonometric polynomial.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(TrigonometricPolynomial a, TrigonometricPolynomial b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial.</param>
        /// <param name="b">The second trigonometric polynomial.</param>
        /// <returns>The result of the operator.</returns>
        public static TrigonometricPolynomial operator +(TrigonometricPolynomial a, TrigonometricPolynomial b)
        {
            if (a == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("b");
            }

            TrigonometricPolynomial minPolynomial = TrigonometricPolynomial.Min(a, b);
            TrigonometricPolynomial result = TrigonometricPolynomial.Max(a, b).Copy();

            for (int i = 0; i < minPolynomial.Degree; i++)
            {
                result[i] += minPolynomial[i];
            }

            return result;
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first trigonometric polynomial.</param>
        /// <param name="b">The second trigonometric polynomial.</param>
        /// <returns>The result of the operator.</returns>
        public static TrigonometricPolynomial operator -(TrigonometricPolynomial a, TrigonometricPolynomial b)
        {
            if (a == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (TrigonometricPolynomial) null)
            {
                throw new ArgumentNullException("b");
            }

            TrigonometricPolynomial minPolynomial = TrigonometricPolynomial.Min(a, b);
            TrigonometricPolynomial result = TrigonometricPolynomial.Max(a, b).Copy();

            for (int i = 0; i < minPolynomial.Degree; i++)
            {
                result[i] -= minPolynomial[i];
            }

            return result;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = this.a0.ToString() + "/2 + ";

            for (int i = 0; i < this.Degree; i++)
            {
                result += "(" + this[i].ToString(i + 1) + ") + ";
            }

            return result.Substring(0, result.Length - 3);
        }
    }
}